package com.stu.controller;

import io.swagger.annotations.Api;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhuzi 2022/6/16 14:33
 */
@Slf4j
@org.springframework.web.bind.annotation.RestController
@RequestMapping("/rest")
@Api(tags = "Redis操作")
public class RestTestController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/r1")
    public void rest() {
        RestReqVO restReqVO = new RestReqVO();
        restReqVO.setAge("1").setName("李斯");
        System.out.println(restReqVO);
        //RestDTOVALUE restDTO = restTemplate.postForObject("http://127.0.0.1:9101/hello/rest/r2", restReqVO, RestDTOVALUE.class);
        //System.out.println(restDTO);
        // 无响应的时候使用这个吧
        String s = restTemplate.postForObject("http://127.0.0.1:9101/hello/rest/r3", restReqVO, String.class);
        System.out.println(StringUtils.isBlank(s));


    }

    @PostMapping("/r2")
    public RestDTO rest2(@RequestBody RestReqVO restReqVO) {
        System.out.println(restReqVO);
        RestDTO restDTO = new RestDTO();
        BeanUtils.copyProperties(restReqVO, restDTO);
        return restDTO;
    }

    @PostMapping("/r3")
    public void rest3(@RequestBody RestReqVO restReqVO) {
        System.out.println(restReqVO);
        RestDTO restDTO = new RestDTO();
        BeanUtils.copyProperties(restReqVO, restDTO);

    }

    @Data
    @Accessors(chain = true)
    static class RestReqVO {

        private String name;
        private String age;

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
        }
    }

    @Data
    @Accessors(chain = true)
    static class RestDTO {

        private String name;
        private String age;

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
        }
    }

    @Data
    @Accessors(chain = true)
    static class RestDTOVALUE {

        private String name;
        private String age;

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
        }
    }

}
